package team.seekers.oj.homework.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.*;
import team.seekers.oj.common.entity.BaseDO;
import team.seekers.oj.contest.dto.ContestProblemListDTO;
import team.seekers.oj.contest.entity.ContestDOField;
import team.seekers.oj.homework.conventer.HomeworkConvertUtils;
import team.seekers.oj.homework.dto.HomeworkProblemListDTO;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(HomeworkDOField.TABLE_NAME)
public class HomeworkDO  extends BaseDO {

    @TableId(value = HomeworkDOField.ID, type = IdType.AUTO)
    private Long id;

    @TableField(HomeworkDOField.USER_ID)
    private Long userId;

    @TableField(HomeworkDOField.COURSE_ID)
    private Long courseId;

    @TableField(HomeworkDOField.TITLE)
    private String title;

    @TableField(HomeworkDOField.PROBLEMS)
    private byte[] problems;

    @TableField(HomeworkDOField.PARTICIPANT_GROUPS)
    private byte[] participantGroups;

    @TableField(HomeworkDOField.GMT_START)
    private Date gmtStart;

    @TableField(HomeworkDOField.GMT_END)
    private Date gmtEnd;

    @TableField(HomeworkDOField.DESCRIPTION)
    private String description;

    @TableField(HomeworkDOField.DELETED)
    @TableLogic(value = "0", delval = "1")
    private Integer deleted;

    @TableField(HomeworkDOField.VERSION)
    @Version
    private Integer version;

    @TableField(value = HomeworkDOField.GMT_CREATE, fill = FieldFill.INSERT)
    private Date gmtCreate;

    @TableField(value = HomeworkDOField.GMT_MODIFIED, fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified;

    public HomeworkProblemListDTO getProblemById(Integer problemIndex) throws IOException, ClassNotFoundException {
        if(problemIndex == null){
            return null;
        }
        List<HomeworkProblemListDTO> homeworkProblemListDTOS = HomeworkConvertUtils.problemsTo(problems);
        if (problemIndex > homeworkProblemListDTOS.size() || problemIndex < 1) {
            return null;
        }
        return homeworkProblemListDTOS.get(problemIndex-1);
    }
}
